import 'package:flutter/material.dart';
import 'package:tree_hole/Tabs.dart';
import 'LoginPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'dart:convert';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart' as http;

class WelcomePage extends StatelessWidget {
// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) :super(key: key);
  @override
  _MyHomePageState createState() {
    return _MyHomePageState();
  }
}
class _MyHomePageState extends State {
  var _getname="";
  var  _getpsw="";
  @override
  void initState() {
    super.initState();
    int count =0;
    const period =const Duration(seconds:1);
    print('currentTime='+DateTime.now().toString());
    Timer.periodic(period, (timer) {
      //到时回调
      print('afterTimer='+DateTime.now().toString());
      count++;
      if (count >=3) {
        //取消定时器，避免无限回调
        timer.cancel();
        timer =null;
        toLoing();
      }
    });
  }
  @override
  void dispose() {
    super.dispose();
  }

  void  toLoing()async{
    SharedPreferences sharedPreferences =await SharedPreferences.getInstance();
    _getname = sharedPreferences.getString('username');
    _getpsw = sharedPreferences.getString('password');
    _login() async {
      var url = "http://121.196.101.7:8080/treehole/user/login";
      int code ;
      String data;
      var response1 = await http.post(url,body: {"username":_getname,"password":_getpsw});
      var response2=json.decode(response1.body);
      code = response2["code"];
      data = response2["data"];
      if(code==0){
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context){
          return Tabs(data);},));
      }else{
        Fluttertoast.showToast(
          msg: "密码已修改，请重新登陆",
          backgroundColor: Colors.amber[50],
          textColor: Colors.black,
          fontSize: 15,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
        );
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context){
          return LoginPage();},));
      }
    }
    if(_getname!=null&&_getpsw!=null){
      _login();
    }else{
      Navigator.of(context).pushReplacement(
          new MaterialPageRoute(builder: (context) {
            return new LoginPage();
          },)
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  Container(
      child: Text('欢 迎',
        style: TextStyle(
          fontSize: 40,
          fontStyle: FontStyle.italic,
        ),),
      alignment: Alignment.center,
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(
        image: new DecorationImage(
          fit: BoxFit.cover,
          image: new AssetImage(
              'images/paper6.jpg'),
        ),
      ),
    );
  }
}